﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Infragistics.Windows.DataPresenter;
using System.Collections;
using BandAid.DalCE;


namespace BandAid.Gui
{
    /// <summary>
    /// Interaction logic for WPFBandAid.xaml
    /// </summary>
    public partial class WPFBandAid : Window
    {
        public static bool isRuntime = false;

        public WPFBandAid()
        {
            isRuntime = true;
            InitializeComponent();

       
        }

        private void dataBind(object sender, RoutedEventArgs e)
        {
           

        }

        /// <summary>
        /// Generic update method to be called from the grid
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void datagrid_RecordUpdated(object sender, Infragistics.Windows.DataPresenter.Events.RecordUpdatedEventArgs e)
        {

            if (e.Record != null && e.Record.DataItem != null)
            {
                ProcessLogic.Instance.UpdateDal();

                
            }

        }

        private void datagridArtist_RecordAdded(object sender, Infragistics.Windows.DataPresenter.Events.RecordAddedEventArgs e)
        {
            if (e.Record != null && e.Record.DataItem != null)
            {

                ARTISTS a = (ARTISTS)e.Record.DataItem;
                a.NAME = "N";
                a.DEFAULT_SIAE_OWNER = "Unkwnown";

                ProcessLogic.Instance.dc.ARTISTS.InsertOnSubmit(a);
                ProcessLogic.Instance.UpdateDal();



            }
        }

      
        /// <summary>
        /// Helper method to reload the grid by resetting the datasource (weird but unique way to force)
        /// </summary>
        /// <param name="grid"></param>
        private void RefreshGrid(Infragistics.Windows.DataPresenter.XamDataGrid grid)
        {

            if (grid.DataSource != null)
            {
                IEnumerable bkp = grid.DataSource;
                grid.DataSource = null;
                grid.DataSource = bkp;
            }
        }

        /// <summary>
        /// When a record in the artist grid is activated we filter its songs in the other one
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void datagridArtist_RecordActivated(object sender, Infragistics.Windows.DataPresenter.Events.RecordActivatedEventArgs e)
        {
            
            DataRecord dr = (DataRecord)e.Record;

            ARTISTS a = dr.DataItem as ARTISTS;

            if (a != null)
            {
                Archives.SongArchive.Clear();

                Archives.SongArchive.AddRange(from x in ProcessLogic.Instance.dc.SONGS where x.ARTIST_ID == a.ID select x);
            }

            this.RefreshGrid(datagridSongs);
        }

      
      
    

        /// <summary>
        /// Method to create new song. We add as artist the currently selected one in the artist grid
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void datagridSongs_RecordAdded(object sender, Infragistics.Windows.DataPresenter.Events.RecordAddedEventArgs e)
        {
            if (e.Record != null && e.Record.DataItem != null)
            {

                if (datagridArtist.ActiveRecord != null)
                {
                    DataRecord dr = (DataRecord)datagridArtist.ActiveRecord;
                    ARTISTS a = (ARTISTS)dr.DataItem;

                    SONGS s = (SONGS)e.Record.DataItem;
                    s.ARTIST_ID = a.ID;
                    ProcessLogic.Instance.dc.SONGS.InsertOnSubmit(s);
                    ProcessLogic.Instance.UpdateDal();


                }
            }
        }

        /// <summary>
        /// Addding new PLACE
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void datagridPlaces_RecordAdded(object sender, Infragistics.Windows.DataPresenter.Events.RecordAddedEventArgs e)
        {
            if (e.Record != null && e.Record.DataItem != null)
            {

               PLACES a = (PLACES)e.Record.DataItem;

                ProcessLogic.Instance.dc.PLACES.InsertOnSubmit(a);
                ProcessLogic.Instance.UpdateDal();



            }
        }

        /// <summary>
        /// Activation of record in places gui will filter binding source of shows
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void datagridPlaces_RecordActivated(object sender, Infragistics.Windows.DataPresenter.Events.RecordActivatedEventArgs e)
        {
            DataRecord dr = (DataRecord)e.Record;

            PLACES a = dr.DataItem as PLACES;

            if (a != null)
            {
                Archives.ShowArchive.Clear();

                Archives.ShowArchive.AddRange(from x in ProcessLogic.Instance.dc.SHOWS where x.PLACE_ID == a.ID select x);
            }

            this.RefreshGrid(datagridShows);
        }


        /// <summary>
        /// Adds show linked to current selected place
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void datagridShows_RecordAdded(object sender, Infragistics.Windows.DataPresenter.Events.RecordAddedEventArgs e)
        {
            if (e.Record != null && e.Record.DataItem != null)
            {

                if (datagridPlaces.ActiveRecord != null)
                {
                    DataRecord dr = (DataRecord)datagridPlaces.ActiveRecord;
                    PLACES a = (PLACES)dr.DataItem;

                    SHOWS s = (SHOWS)e.Record.DataItem;
                    s.PLACE_ID = a.ID;
                    ProcessLogic.Instance.dc.SHOWS.InsertOnSubmit(s);
                    ProcessLogic.Instance.UpdateDal();


                }
            }
        }


        /// <summary>
        /// Filling datasource for songs performed at a certain show
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void datagridShows_RecordActivated(object sender, Infragistics.Windows.DataPresenter.Events.RecordActivatedEventArgs e)
        {

            DataRecord dr = (DataRecord)e.Record;

            SHOWS a = dr.DataItem as SHOWS;

            if (a != null)
            {
                Archives.ShowSongArchive.Clear();

                
                     

                Archives.ShowSongArchive.AddRange(from x in ProcessLogic.Instance.dc.SHOW_SONGS where x.SHOW_ID == a.ID select x);
            }

            this.RefreshGrid(datagridShowSongs);

        }


        /// <summary>
        /// Button adds songs
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DataRecord placeRecord = (DataRecord) datagridShows.ActiveRecord;

            if (placeRecord == null)
            {
                MessageBox.Show("Choose a place first!");
            }

            SHOWS show = (SHOWS) placeRecord.DataItem;

            foreach (Record r in datagridSongs.SelectedItems.Records)
            {
                
                    DataRecord dr = (DataRecord)r;

                    if (dr != null)
                    {
                        SONGS song = (SONGS)dr.DataItem;

                        SHOW_SONGS ss = new SHOW_SONGS();

                        ss.SONG_ID = song.ID;
                        ss.SHOW_ID = show.ID;
                        ss.NUMBER = Archives.ShowSongArchive.Count + 1;
                        
                        
                        try
                        {
                        ProcessLogic.Instance.dc.SHOW_SONGS.InsertOnSubmit(ss);
                        ProcessLogic.Instance.UpdateDal();
                        }
                        catch(System.Data.Linq.DuplicateKeyException)
                        {

                        }
                        finally
                        {
                            Archives.ShowSongArchive.Clear();
                            Archives.ShowSongArchive.AddRange(from x in ProcessLogic.Instance.dc.SHOW_SONGS where x.SHOW_ID == show.ID select x);
                            this.RefreshGrid(datagridShowSongs);

                        }
                        
                    }
                
            }
        }

        /// <summary>
        /// Clone the current selected show to playlist planner
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Clone_Current_Show(object sender, RoutedEventArgs e)
        {
            Archives.NewPlaylistArchive.AddRange(Archives.GetShowSongsArchive());

            this.RefreshGrid(datagridNewPlaylist);
        }

        /// <summary>
        /// Adds to playlist selected songs
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Add_Current_Song(object sender, RoutedEventArgs e)
        {
            foreach (Record r in datagridSongs.SelectedItems.Records)
            {

                DataRecord dr = (DataRecord)r;

                if (dr != null)
                {
                    SONGS song = (SONGS)dr.DataItem;
                    SHOW_SONGS ss = new SHOW_SONGS();

                    ss.SONG_ID = song.ID;
                    ss.NUMBER = Archives.NewPlaylistArchive.Count + 1;

                    Archives.NewPlaylistArchive.Add(ss);

                }

                this.RefreshGrid(datagridNewPlaylist);
            }
        }


        /// <summary>
        /// Trash the playlist
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Playlist_Discard(object sender, RoutedEventArgs e)
        {
            Archives.NewPlaylistArchive.Clear();
            this.RefreshGrid(datagridNewPlaylist);
        }

        /// <summary>
        /// Fixes the current playlist into a real show 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Save_Playlist_To_Show(object sender, RoutedEventArgs e)
        {
            PLACES showPlace = null;
            if (newShowPlace.SelectedItem != null)
            {
                showPlace = (PLACES)newShowPlace.SelectedItem;
            }

            if (showPlace == null)
                throw new NullReferenceException("Show cannot be NULL, something is really wrong!");

            DateTime date = DateTime.Today;

            if (newShowDate.Text != null && newShowDate.Text != string.Empty)
            {
                DateTime.TryParse(newShowDate.Text, out date);
            }

            if (date <= DateTime.MinValue || date >= DateTime.MaxValue)
                date = DateTime.Today;

            SHOWS show = new SHOWS();
                  
            show.PLACE_ID = showPlace.ID;
            show.DATE = date;

            ProcessLogic.Instance.dc.SHOWS.InsertOnSubmit(show);
            ProcessLogic.Instance.UpdateDal();

            foreach (SHOW_SONGS s in Archives.NewPlaylistArchive)
            {
                SHOW_SONGS ss = new SHOW_SONGS();
                ss.SHOW_ID = show.ID;
                ss.SONG_ID = s.SONG_ID;
                ss.NUMBER = s.NUMBER;

                ProcessLogic.Instance.dc.SHOW_SONGS.InsertOnSubmit(ss);
                ProcessLogic.Instance.UpdateDal();
               
            }

            ProcessLogic.Instance.UpdateDal();

            Archives.NewPlaylistArchive.Clear();
            Archives.ShowArchive.Clear();


            this.RefreshGrid(datagridShows);
            this.RefreshGrid(datagridNewPlaylist);
        }

        /// <summary>
        /// Handles deletion of artists plus its songs
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void datagridArtist_RecordsDeleting(object sender, Infragistics.Windows.DataPresenter.Events.RecordsDeletingEventArgs e)
        {
            foreach (Record r in e.Records)
            {
                DataRecord dr = (DataRecord)r;

                if (dr != null)
                {
                    ARTISTS a = (ARTISTS)dr.DataItem;
                                  

                    ProcessLogic.Instance.DeleteArtist(a);
                }



            }

            this.RefreshGrid(datagridArtist);
        }

       

        
    }
}
